package org.karelun.gef.commands;

import org.karelun.gef.helper.ConstantMessages;
import org.karelun.gef.model.AbstractConnectionModel;
import org.karelun.gef.model.AttributeModel;
import org.karelun.gef.model.MethodModel;
import org.karelun.gef.model.Node;

public class ConnectionCreateCommand extends AbstractCommand {

	private AbstractConnectionModel connection;

	private Node source;

	private Node target;

	public boolean canExecute() {
		if (source == null || target == null)
			return false;
		else if (source.equals(target))
			return false;
		else if (source instanceof AttributeModel
				|| source instanceof MethodModel)
			return false;
		else if (target instanceof AttributeModel
				|| target instanceof MethodModel)
			return false;
		return true;
	}

	public void execute() {
		connection.attachSource();
		connection.attachTarget();
		connection.addToDiagram();
	}

	public void setConnection(Object model) {
		connection = (AbstractConnectionModel) model;
	}

	public void setSource(Object model) {
		source = (Node) model;
		connection.setSource(source);
	}

	public void setTarget(Object model) {
		target = (Node) model;
		connection.setTarget(target);
	}
	
	public Node getSource(){
		return source;
	}
	
	public Node getTarget(){
		return target;
	}
	
	public void undo() {
		connection.detachSource();
		connection.detachTarget();
		connection.removeToDiagram();
	}

	public String getLabel() {
		return ConstantMessages.CreateConnCommand_Label_Tex; 
	}

	@Override
	public void redo() {
		execute();
	}
}